home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / ErrorLogFetch.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  4.6 KB  |  132 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008 Red Hat, Inc.
  6. ## Copyright (C) 2008 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import cups
  23. import os
  24. import tempfile
  25. import time
  26. from timedops import TimedOperation
  27. from base import *
  28. class ErrorLogFetch(Question):
  29.     def __init__ (self, troubleshooter):
  30.         Question.__init__ (self, troubleshooter, "Error log fetch")
  31.         troubleshooter.new_page (gtk.Label (), self)
  32.         self.persistent_answers = {}
  33.  
  34.     def display (self):
  35.         answers = self.troubleshooter.answers
  36.         parent = self.troubleshooter.get_window ()
  37.         self.answers = {}
  38.         try:
  39.             checkpoint = answers['error_log_checkpoint']
  40.         except KeyError:
  41.             checkpoint = None
  42.  
  43.         if self.persistent_answers.has_key ('error_log'):
  44.             checkpoint = None
  45.  
  46.         def fetch_log (c):
  47.             prompt = c._get_prompt_allowed ()
  48.             c._set_prompt_allowed (False)
  49.             c._connect ()
  50.             (tmpfd, tmpfname) = tempfile.mkstemp ()
  51.             os.close (tmpfd)
  52.             success = False
  53.             try:
  54.                 c.getFile ('/admin/log/error_log', tmpfname)
  55.                 success = True
  56.             except cups.HTTPError:
  57.                 try:
  58.                     os.remove (tmpfname)
  59.                 except OSError:
  60.                     pass
  61.  
  62.             c._set_prompt_allowed (prompt)
  63.             if success:
  64.                 return tmpfname
  65.             return None
  66.  
  67.         self.authconn = self.troubleshooter.answers['_authenticated_connection']
  68.         if checkpoint != None:
  69.             self.op = TimedOperation (fetch_log,
  70.                                       (self.authconn,),
  71.                                       parent=parent)
  72.             tmpfname = self.op.run ()
  73.             if tmpfname != None:
  74.                 f = file (tmpfname)
  75.                 f.seek (checkpoint)
  76.                 lines = f.readlines ()
  77.                 os.remove (tmpfname)
  78.                 self.answers = { 'error_log': map (lambda x: x.strip (),
  79.                                                    lines) }
  80.  
  81.         if answers.has_key ('error_log_debug_logging_set'):
  82.             try:
  83.                 self.op = TimedOperation (self.authconn.adminGetServerSettings,
  84.                                           parent=parent)
  85.                 settings = self.op.run ()
  86.             except cups.IPPError:
  87.                 return False
  88.  
  89.             settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '0'
  90.             orig_settings = answers['cups_server_settings']
  91.             settings['MaxLogSize'] = orig_settings.get ('MaxLogSize', '2000000')
  92.             success = False
  93.             def set_settings (connection, settings):
  94.                 connection.adminSetServerSettings (settings)
  95.  
  96.                 # Now reconnect.
  97.                 attempt = 1
  98.                 while attempt <= 5:
  99.                     try:
  100.                         time.sleep (1)
  101.                         connection._connect ()
  102.                         break
  103.                     except RuntimeError:
  104.                         # Connection failed
  105.                         attempt += 1
  106.  
  107.             try:
  108.  
  109.                 self.op = TimedOperation (set_settings,
  110.                                           (self.authconn, settings),
  111.                                           parent=parent)
  112.                 self.op.run ()
  113.                 self.persistent_answers['error_log_debug_logging_unset'] = True
  114.             except cups.IPPError:
  115.                 pass
  116.  
  117.         return False
  118.  
  119.     def collect_answer (self):
  120.         answers = self.persistent_answers.copy ()
  121.         answers.update (self.answers)
  122.         return answers
  123.  
  124.     def cancel_operation (self):
  125.         self.op.cancel ()
  126.  
  127.         # Abandon the CUPS connection and make another.
  128.         answers = self.troubleshooter.answers
  129.         factory = answers['_authenticated_connection_factory']
  130.         self.authconn = factory.get_connection ()
  131.         self.answers['_authenticated_connection'] = self.authconn
  132.